//
//  CustomAlertViewController.m
//  We365
//
//  Created by Bui Thien An on 12/16/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomAlertViewController.h"

@implementation CustomAlertViewController

@synthesize  caller, context, data, images;

-(id)initWithCaller:(id<AlertTableViewDelegate>)_caller data:(NSArray*)_data images:(NSArray *)_images title:(NSString*)_title andContext:(id)_context {
    NSMutableString *messageString = [NSMutableString stringWithString:@"\n"];
    tableHeight = 0;
    
    [messageString setString:@"\n\n\n\n\n\n\n\n\n\n"];    
    tableHeight = 207;        
    
    if(self = [super initWithTitle:_title message:messageString delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]){
        self.caller = _caller;
        self.context = _context;
        self.data = _data;
        self.images = _images;
        [self prepare];        
    }
    return self;    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self.caller didSelectRowAtIndex:-1 withContext:self.context];    
}

-(void)show{
    self.hidden = YES;
    [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(myTimer:) userInfo:nil repeats:NO];
    [super show];    
}

-(void)myTimer:(NSTimer*)_timer{
    self.hidden = NO;
    [myTableView flashScrollIndicators];    
}

-(void)prepare{
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(11, 50, 261, tableHeight) style:UITableViewStylePlain];
    if([data count] <= 5){
        myTableView.scrollEnabled = NO;        
    }
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self addSubview:myTableView];
    
    UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(11, 50, 261, 4)] autorelease];
    imgView.image = [UIImage imageNamed:@"top.png"];
    [self addSubview:imgView];
    
    imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(11, tableHeight+46, 261, 4)] autorelease];
    imgView.image = [UIImage imageNamed:@"bottom.png"];
    [self addSubview:imgView];
    
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 10);
    [self setTransform:myTransform];    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ABC"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ABC"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    
    cell.textLabel.text = [data objectAtIndex:[indexPath row]];
    NSString *imageName = [images objectAtIndex:[indexPath row]];
    cell.imageView.image = [UIImage imageNamed:imageName];
    return cell;    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dismissWithClickedButtonIndex:0 animated:YES];
    [self.caller didSelectRowAtIndex:indexPath.row withContext:self.context];    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [data count];    
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 42;
}

-(void)dealloc{
    self.data = nil;
    self.caller = nil;
    self.context = nil;
    self.images = nil;
    [myTableView release];
    [super dealloc];    
}


@end
